package com.neiquan.backstage.platform_alliance_member.web.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.BackendErrorCode;
import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Paging;
import com.neiquan.backstage.platform_alliance_member.PlatformAllianceMemberService;
import com.neiquan.backstage.pub.bsc.dao.po.PlatformAllianceMember;

@Controller
@RequestMapping(value = "platformAllianceMember", produces = { "text/json;charset=UTF-8" })
public class PlatformAllianceMemberAction {
	private static Class<?> poc = PlatformAllianceMember.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private PlatformAllianceMemberService allianceMemberService;
	
	@ResponseBody
	@RequestMapping("queryPlatformAllianceMember")
	public String queryPlatformAllianceMember() {
		List<PlatformAllianceMember> paging = (List<PlatformAllianceMember>) extraSpringHibernateTemplate.findAll(poc);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
	
	@ResponseBody
	@RequestMapping("list")
	public String list(int pageIndex, int pageSize) {
		Paging<?> paging = extraSpringHibernateTemplate.findPage(poc, Order.desc("memberId"), pageIndex * pageSize, pageSize);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String name, String provinceCode, String dtCreatBegin, String dtCreatEnd) throws ParseException {
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(name)) {
			criterions.add(Restrictions.like("name", "%" + name + "%"));
		}
		if (StringUtils.isNoneBlank(provinceCode)) {
			criterions.add(Restrictions.eq("provinceCode", provinceCode));
		}
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		if (StringUtils.isNoneBlank(dtCreatBegin)) {
			criterions.add(Restrictions.ge("dtCreat", dateFormat.parse(dtCreatBegin).getTime()));
		}
		if (StringUtils.isNoneBlank(dtCreatEnd)) {
			criterions.add(Restrictions.le("dtCreat", dateFormat.parse(dtCreatEnd).getTime()));
		}
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("memberId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "remove")
	public String remove(Integer id) {
		PlatformAllianceMember toDelete = allianceMemberService.queryById(id);
		if (toDelete == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		extraSpringHibernateTemplate.getHibernateTemplate().delete(toDelete);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "queryDetail")
	public String queryDetail(Integer id) {
		return CommonResp.buildSuccessResp(allianceMemberService.queryById(id)).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "updateEntrepreneurServiceUrl")
	public String updateEntrepreneurServiceUrl(Integer id, String entrepreneurServiceUrl, String remark) {
		PlatformAllianceMember toUpdate = allianceMemberService.queryById(id);
		if (null == toUpdate) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		if (StringUtils.isNoneBlank(entrepreneurServiceUrl)) {
			toUpdate.setEntrepreneurServiceUrl(entrepreneurServiceUrl.trim());
		}
		if (StringUtils.isNoneBlank(remark)) {
			toUpdate.setRemark(remark.trim());
		}
		extraSpringHibernateTemplate.getHibernateTemplate().update(toUpdate);
		return CommonResp.buildSuccessResp().toJsonString();
	}
}
